#include "options.h"

Options::Options()
{
	_toolsWindow = new QDialog();
}

#if defined __linux__
extern "C" __attribute__((visibility ("default"))) IModule* getOptions()
#else
extern "C" __declspec(dllexport) IModule* getOptions()
#endif
{
	return new Options();
}

void					Options::setApi(BabelApi *api)
{
	_api = api;
}

void					Options::takeOrder(IOrder *order, Protocol proto)
{
	order = order;
	proto = proto;
}

void					Options::init()
{
	addMenuOptions();
	_api->gui->addMenu(_menu);
	std::cout << "DLL OPTIONS !!!!!!!!!" << std::endl;
}

void					Options::addMenuOptions()
{
	_menu = new QMenu("Options");
	_tools = _menu->addAction("tools");
	_support = _menu->addAction("support");

	connect(_tools, SIGNAL(triggered()), this, SLOT(OnToolsButtonTriggered()));
	connect(_support, SIGNAL(triggered()), this, SLOT(OnSupportButtonTriggered()));
}

void					Options::OnToolsButtonTriggered()
{
	if (_toolsWindow->isVisible() == false)
	{
		QVBoxLayout			*MainLayout = new QVBoxLayout();
		QVBoxLayout			*SpeakerLayout = new QVBoxLayout();
		QVBoxLayout			*MicrophoneLayout = new QVBoxLayout();
		QHBoxLayout			*layoutSliderSpeaker = new QHBoxLayout();
		QHBoxLayout			*layoutSliderMicrophone = new QHBoxLayout();
		QHBoxLayout			*layoutMuteSpeaker = new QHBoxLayout();
		QHBoxLayout			*layoutMuteMicrophone = new QHBoxLayout();
		QLabel				*Speaker = new QLabel("Speaker ");
		QLabel				*Microphone = new QLabel("Microphone ");
		QLabel				*MuteSpeakerL = new QLabel("Mute ");
		QLabel				*MuteMicrophoneL = new QLabel("Mute ");
		QCheckBox			*MuteSpeaker = new QCheckBox();
		QCheckBox			*MuteMicrophone = new QCheckBox();

		_exitButton = new QPushButton();
		sliderMicrophone = new QSlider(Qt::Horizontal, 0);
		sliderSpeaker = new QSlider(Qt::Horizontal, 0);
		sliderSpeaker->setStyleSheet(" QSlider::groove:horizontal {"
			"border: 1px solid #999999;"
			"height: 2px;"
			"background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 white);"
			"margin: 2px 0;"
		"}"

		"QSlider::handle:horizontal {"
			"background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b0b0b0, stop:1 white);"
			"border: 1px solid #5c5c5c;"
			"width: 20px;"
			"margin: -6px 0;"
			"border-radius: 5px;"
		"}"
		);
		sliderMicrophone->setStyleSheet(" QSlider::groove:horizontal {"
		    "border: 1px solid #999999;"
		    "height: 2px;"
		    "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 white);"
		    "margin: 2px 0;"
		"}"

		"QSlider::handle:horizontal {"
        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b0b0b0, stop:1 white);"
        "border: 1px solid #5c5c5c;"
        "width: 20px;"
        "margin: -6px 0;"
        "border-radius: 5px;"
    "}"
	);
		_exitButton->setText("Exit");

		layoutSliderSpeaker->addWidget(Speaker);
		layoutSliderSpeaker->addWidget(sliderSpeaker);

		layoutMuteSpeaker->addWidget(MuteSpeakerL);
		layoutMuteSpeaker->addWidget(MuteSpeaker);

		SpeakerLayout->addLayout(layoutSliderSpeaker);
		SpeakerLayout->addLayout(layoutMuteSpeaker);

		layoutSliderMicrophone->addWidget(Microphone);
		layoutSliderMicrophone->addWidget(sliderMicrophone);

		layoutMuteMicrophone->addWidget(MuteMicrophoneL);
		layoutMuteMicrophone->addWidget(MuteMicrophone);

		MicrophoneLayout->addLayout(layoutSliderMicrophone);
		MicrophoneLayout->addLayout(layoutMuteMicrophone);

		MainLayout->addLayout(SpeakerLayout);
		MainLayout->addLayout(MicrophoneLayout);

		MainLayout->addWidget(_exitButton);

		connect(sliderSpeaker, SIGNAL(valueChanged(int)), this, SLOT(SpeakerValueChanged(int)));
		connect(sliderMicrophone, SIGNAL(valueChanged(int)), this, SLOT(MicrophoneValueChanged(int)));

		connect(MuteSpeaker, SIGNAL(stateChanged(int)), this, SLOT(SpeakerStateChanged(int)));
		connect(MuteMicrophone, SIGNAL(stateChanged(int)), this, SLOT(MicrophoneStateChanged(int)));

		connect(_exitButton, SIGNAL(clicked()), this, SLOT(closeDialog()));

		_toolsWindow->setLayout(MainLayout);
		_toolsWindow->setWindowTitle("Tools");
		_toolsWindow->setFixedSize(300, 200);
		_toolsWindow->show();
	}
}

void					Options::closeDialog()
{
	_toolsWindow->close();
}

void					Options::SpeakerStateChanged(int state)
{
	if (state)
	{
		sliderSpeaker->setDisabled(true);
		sliderSpeaker->setStyleSheet(" QSlider::groove:horizontal {"
        "border: 1px solid grey;"
        "height: 2px;"
        "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 grey, stop:1 grey);"
        "margin: 2px 0;"
    "}"

    "QSlider::handle:horizontal {"
        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 grey, stop:1 grey);"
        "border: 1px solid grey;"
        "width: 20px;"
        "margin: -6px 0;"
        "border-radius: 5px;"
    "}"
	);
	}
	else
	{
		sliderSpeaker->setDisabled(false);
		sliderSpeaker->setStyleSheet(" QSlider::groove:horizontal {"
        "border: 1px solid #999999;"
        "height: 2px;"
        "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 white);"
        "margin: 2px 0;"
    "}"

    "QSlider::handle:horizontal {"
        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b0b0b0, stop:1 white);"
        "border: 1px solid #5c5c5c;"
        "width: 20px;"
        "margin: -6px 0;"
        "border-radius: 5px;"
    "}"
	);
	}
}

void					Options::MicrophoneStateChanged(int state)
{
	if (state)
	{
		sliderMicrophone->setDisabled(true);
		sliderMicrophone->setStyleSheet(" QSlider::groove:horizontal {"
        "border: 1px solid grey;"
        "height: 2px;"
        "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 grey, stop:1 grey);"
        "margin: 2px 0;"
    "}"

    "QSlider::handle:horizontal {"
        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 grey, stop:1 grey);"
        "border: 1px solid grey;"
        "width: 20px;"
        "margin: -6px 0;"
        "border-radius: 5px;"
    "}"
	);
	}
	else
	{
		std::cout << "PAS MUTE" << std::endl;
		sliderMicrophone->setDisabled(false);
		sliderMicrophone->setStyleSheet(" QSlider::groove:horizontal {"
        "border: 1px solid #999999;"
        "height: 2px;"
        "background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #B1B1B1, stop:1 white);"
        "margin: 2px 0;"
    "}"

    "QSlider::handle:horizontal {"
        "background: qlineargradient(x1:0, y1:0, x2:1, y2:1, stop:0 #b0b0b0, stop:1 white);"
        "border: 1px solid #5c5c5c;"
        "width: 20px;"
        "margin: -6px 0;"
        "border-radius: 5px;"
    "}"
);
	}
}

void					Options::SpeakerValueChanged(int volume)
{
	std::cout << "Speaker Volume is : " << volume << std::endl;
}

void					Options::MicrophoneValueChanged(int volume)
{
	std::cout << "Microphone Volume is : " << volume << std::endl;
}

void					Options::test()
{
	std::cout << "SUCCESS TEST" << std::endl;
}

void					Options::OnSupportButtonTriggered()
{
	QMessageBox			*MsgBox = new QMessageBox();

	MsgBox->setText("this is the babel's blue team, if you\nhave any question you can contact our support");
	MsgBox->setWindowTitle("Support");
	MsgBox->show();
}

std::vector<Protocol>	Options::getProtocol() const
{
	return _proto;
}

Options::~Options()
{

}
